
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <fcntl.h>
#include <string>




using namespace std;
int main(int argc, char *argv[]){
	Record *record;
 cout<< "opcja: "<<*argv[1]<<endl;
 int fd, fk;
 fd=open("nowy.txt", O_CREAT | O_RDWR, 07777);//domyslnie jest do pisania czytania
 fk=open("lkrot.txt", O_CREAT | O_RDWR, 07777);//domyslnie jest do pisania czytania

 
 DesMenager *desMen =new DesMenager(fd);
 if(*argv[1]=='z'){//zapis
cout <<"wybrales zapis"<<endl;
	 Command *c1,*c2,*c3, *c4;
	 c1=new Command(1,11,1.1,1.2,"ala ma kota");
	 c2=new Command(2,44,5.7,3.8,"zielony kastan na czerwonym drzewie");
	 c3=new Command(0,(void *)new int(3),
			  0,(void *)new int(534),
			  1,(void *)new float(54.523),
			  1,(void *)new float(86.4),  2,(void *) new string("kot"));
	 c4=new Command(0,(void *)new int(3),
			  2,(void *)new string("duzo sniegu"),
			     1,(void *)new float(111.523),
			      2,(void *) new string("napadalo wczoraj"),
				 1,(void *)new float(223.14));
	saveNew(c3, desMen, fd, fk);
 }
 else if(*argv[1]=='t'){//tworz
	 cout<<"resize"<<endl;
	 desMen->resize(10);
	 cout <<"tworzy puste"<<endl;
	 desMen->createEmptyDes(10, 0);
	 lseek(fk, 10000, SEEK_END);
	 write(fk, "q", 1);
 }   
 else if(*argv[1]=='d'){
	 deleteRec(1, desMen);
 } 
 else if(*argv[1]=='s'){
	 Command *c;
	/* c1=new CommandIn(1,11,1.1,1.2,"ala ma kota");
	 c2=new CommandIn(2,44,5.7,3.8,"zielony kastan na czerwonym drzewie");
	 c3=new CommandIn(0,(void *)new int(3),
			  0,(void *)new int(534),
			     1,(void *)new float(54.523),
				1,(void *)new float(86.4),  2,(void *) new string("kot"));
	 c4=new CommandIn(0,(void *)new int(3),
			  2,(void *)new string("duzo sniegu"),
			     1,(void *)new float(111.523),
				2,(void *) new string("napadalo wczoraj"),
				   1,(void *)new float(223.14));*/
	c=new Command(
			2,(void *)new string("duzo sniegu"),
			1,(void *)new float(111.523)
			);
	search(c, desMen, fk);
 } 
 else{//o-odczyt
	int dl=lseek(fd,0,SEEK_END);

	char *buf=new char[DESLENGTH];
	for(int i=0;(i*DESLENGTH)<dl;++i){
		RecordDescriptor des;
		Tuple k;
		cout <<"@@@@@@@@@@@@        KROTKA"<<i<<"       @@@@@@@@@@@@@@@@@"<<endl;
		int of=lseek(fd,(i*DESLENGTH), SEEK_SET);
		read(fd, buf, DESLENGTH);
		des.createFromBuf(buf);
		//des.wypisz();
		if(des.getBooked()){unsigned int size=des.getSize();
		des.wypisz();
		char *bufK=new char[size];
		lseek(fk, des.getOfset(), SEEK_SET);
		read(fk, bufK, size);
		k.createFromBuf(bufK, des.getType());
		k.wypisz();}
	}
 }
 close(fd);
 close(fk);
}
